<template>
  <div class="dashboard">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="250px">
        <div class="sidebar">
          <div class="logo">
            <h2>管理系统</h2>
          </div>
          
          <el-menu
            :default-active="activeMenu"
            class="sidebar-menu"
            router
            unique-opened
          >
            <el-menu-item index="/dashboard">
              <el-icon><House /></el-icon>
              <span>仪表盘</span>
            </el-menu-item>
            
            <el-menu-item 
              index="/users"
              v-if="authStore.hasPermission('user:read')"
            >
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            
            <el-menu-item 
              index="/roles"
              v-if="authStore.hasPermission('role:read')"
            >
              <el-icon><UserFilled /></el-icon>
              <span>角色管理</span>
            </el-menu-item>
            
            <el-menu-item 
              index="/permissions"
              v-if="authStore.hasPermission('permission:read')"
            >
              <el-icon><Key /></el-icon>
              <span>权限管理</span>
            </el-menu-item>
            
            <el-menu-item 
              index="/logs"
              v-if="authStore.hasPermission('log:read')"
            >
              <el-icon><Document /></el-icon>
              <span>系统日志</span>
            </el-menu-item>
          </el-menu>
        </div>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header height="60px">
          <div class="header">
            <div class="header-left">
              <h3>{{ pageTitle }}</h3>
            </div>
            
            <div class="header-right">
              <el-dropdown @command="handleCommand">
                <div class="user-info">
                  <el-avatar :size="32" :src="authStore.user?.avatar">
                    {{ authStore.user?.nickname?.[0] || authStore.user?.username?.[0] }}
                  </el-avatar>
                  <span class="username">{{ authStore.user?.nickname || authStore.user?.username }}</span>
                  <el-icon><ArrowDown /></el-icon>
                </div>
                
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="profile">
                      <el-icon><User /></el-icon>
                      个人资料
                    </el-dropdown-item>
                    <el-dropdown-item command="logout" divided>
                      <el-icon><SwitchButton /></el-icon>
                      退出登录
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        
        <!-- 主要内容 -->
        <el-main>
          <div class="main-content">
            <router-view />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// 页面标题
const pageTitle = computed(() => {
  const titles: Record<string, string> = {
    '/dashboard': '仪表盘',
    '/users': '用户管理',
    '/roles': '角色管理',
    '/permissions': '权限管理',
    '/logs': '系统日志'
  }
  return titles[route.path] || '管理系统'
})

/**
 * 处理下拉菜单命令
 * @param command 命令
 */
const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      // TODO: 打开个人资料对话框
      ElMessage.info('个人资料功能开发中')
      break
    case 'logout':
      await handleLogout()
      break
  }
}

/**
 * 处理退出登录
 */
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    await authStore.logout()
    ElMessage.success('已退出登录')
    router.push('/login')
  } catch (error) {
    // 用户取消操作
  }
}
</script>

<style scoped>
.dashboard {
  height: 100vh;
}

.sidebar {
  height: 100vh;
  background-color: #304156;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #2b3a4b;
  color: white;
  border-bottom: 1px solid #3a4a5c;
}

.logo h2 {
  font-size: 18px;
  font-weight: 600;
}

.sidebar-menu {
  border: none;
  background-color: #304156;
}

.sidebar-menu .el-menu-item {
  color: #bfcbd9;
  border-bottom: 1px solid #3a4a5c;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #3a4a5c;
  color: #409eff;
}

.sidebar-menu .el-menu-item.is-active {
  background-color: #409eff;
  color: white;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
  background-color: white;
  border-bottom: 1px solid #e6e6e6;
}

.header-left h3 {
  color: #333;
  font-size: 18px;
  font-weight: 500;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.username {
  margin: 0 8px;
  color: #333;
  font-size: 14px;
}

.main-content {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: calc(100vh - 120px);
}
</style>